package cn.bgs.mq.consumer;

import cn.bgs.serviceedu.entity.Customer;
import cn.bgs.serviceedu.entity.Orders;
import cn.bgs.serviceedu.service.CustomerService;
import cn.bgs.serviceedu.service.OrdersService;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.RedisUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/10/13-14:33
 *  用户上传图书后发邮件提醒用户
 */
@Component
@Slf4j
public class BookConsumer {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private OrdersService ordersService;


    //接收订单延时消息
    @RabbitListener(queues = "shop.queue")
    public void message(Message message,Channel channel,@Header(AmqpHeaders.DELIVERY_TAG)long tag,@Header(AmqpHeaders.REDELIVERED) boolean reDelivered) throws IOException {
        String msg = new String(message.getBody());
        Orders byIdOrders = ordersService.findByIdOrders(msg);
        Customer byId = customerService.getById(byIdOrders.getCustomerId());
        byIdOrders.getPkId();
            //查询订单号是否相等
        if (msg.equalsIgnoreCase((String) redisUtils.get(msg))){
            //查询订单状态
        if (byIdOrders.getState()==0){
                try {
                    redisUtils.del(msg);
                    byIdOrders.setState(2);
                    ordersService.updateById(byIdOrders);
//                    customerService.sendMail("您的订单因未在指定时间支付已过期"+new Date().toString(),
//                            byId.getCustomerEmail());
                    channel.basicAck(tag,false);
                    log.info("订单超时未支付,订单号：{}",msg);
                } catch (IOException e) {
                         if (reDelivered){
                             log.info("消息重试失败放入死信队列");
                             //消息如果重复消费就不再进入队列，进入死信队列
                             channel.basicReject(tag,false);
                         }
                        log.info("消息重试");
                         //重回队列重试
                        channel.basicNack(tag,false,true);
                    throw new CSException(20001,"订单状态修改失败");
                }
            }else{
                 redisUtils.del(msg);
                channel.basicAck(tag,false);
                log.info("订单完成,订单号：{}",msg);
            }
        }else{
            log.info("订单错误");
        }



    }


    //接收下单消息
    @RabbitListener(queues = "email.queue")
    public void messageShop(Message message, Channel channel,@Header(AmqpHeaders.DELIVERY_TAG)long tag,@Header(AmqpHeaders.REDELIVERED) boolean reDelivered) throws IOException {

        try {
            String msg = new String(message.getBody());
            Customer byId = customerService.getById(msg);
            log.info("当前时间：{}，收到email.queue队列消息：{}",new Date().toString(),msg);
//            customerService.sendMail("您已经下单与医生的聊天订单,为了您更方便的聊天体验,请您尽快支付,感谢您的支持",
//                    byId.getCustomerEmail());
            channel.basicAck(tag,false);
        } catch (Exception e) {
            if (reDelivered){
                log.info("消息重试失败放入死信队列");
                channel.basicReject(tag,false);
            }
            log.info("消息重试");
            channel.basicNack(tag,false,true);
            throw new CSException(20001,"通知失败，请重试");
        }


    }


}
